classdef translators < handle
    properties
        
    end
    
    methods(Static)
        function s = x2s(obj, x)
        % Translate a vector to a
        % struct, where the fields in the struct are determined by
        % :code:`obj.varnames`.
        %
        % Args:
        %     x (double): vector where the entries
        %         correspond to the order in :code:`obj.varnames` 
            assert(length(x)==length(obj.varnames),...
                'wrong number of entries in x')
            for i=1:length(obj.varnames)
                 s.(obj.varnames{i}) = x(i);
            end
        end
        
        function x = s2x(obj, s)
        % Translate a struct with parameters to a
        % vector. The order in the vector is based on the order in
        % :code:`obj.varnames`.
        %
        % Args:
        %     s (struct): struct with key-value pairs, where the
        %         keys correspond to :code:`obj.varnames`   
            

            assert(length(fieldnames(s))==length(obj.varnames),...
                'wrong number of entries in s')
            x = zeros(length(obj.varnames),1);
            for i=1:length(obj.varnames)
                x(i) =  s.(obj.varnames{i});
            end
        end
        
        function idx = varname2idx(obj, varname)
        % Finds the vector index which corresponds to :code:`varname`
        %
        % Args:
        %     varname (string): key for which to find the index
        % Returns:
        %     int: index corresponding to :code:`varname`
            idx = find(strcmp(obj.varnames, varname));
        end
    end
    
end
